PRO CLEAN
; Add comments.
; More comments.

C=[0,1,-1,-1,1,-1,1,1,-1,1,1,1,-1,1,-1,$
   1,1,1,-1,-1,-1,-1,-1,-1,1,1,-1,-1,1,$
   1,-1,-1,-1,-1,-1,-1,1,1,1,-1,1,-1,1,$
   1,1,-1,1,1,-1,1,-1,-1,1]

A=INTARR(53,53)
A=(C#C+1)/2
A[*,0]=0
A[0,1:52]=1

M=INTARR(95,95)
M[0:47,0:47]=ROTATE(A[0:47,0:47],2)
M[48:94,0:47]=ROTATE(A[1:47,0:47],7)
M[0:47,48:94]=ROTATE(A[0:47,1:47],5)
M[48:94,48:94]=A[1:47,1:47]

S=INTARR(189,189)
S[25,27]=18
S[65,70]=13
S[50,63]=16


B=RANDOMU(1,189,189,POISSON=1)


G=M+M-1
G[47,47]=1

D=CONVOL(S,M,CENTER=0,/EDGE_ZERO)+B

D0=INTARR(283,283)
D0[0:188,0:188]=D

S1=CONVOL(D0,G,/EDGE_ZERO,CENTER=0)

M0=INTARR(189,189)
M0[0:94,0:94]=M
MG=CONVOL(M0,G,/EDGE_ZERO,CENTER=0)

CLEAN=INTARR(283,283)
CC=0
REPEAT BEGIN
P=S1[0,0]
U=0
V=0
DIF=INTARR(283,283)
FOR i=0,282 DO BEGIN
 FOR j=0,282 DO BEGIN
  IF S1[j,i] GT P THEN BEGIN
    P=S1[j,i]
    U=i
    V=j
  ENDIF 
 ENDFOR
ENDFOR
DIF[V,U]=0.03*P
S10=INTARR(480,480)
S10[94:376,94:376]=S1
MG0=INTARR(480,480)
MG0[V:V+188,U:U+188]=0.03*MG
S10=S10-MG0
S1=S10[94:376,94:376]
CLEAN=CLEAN+DIF
CC=CC+1
ENDREP UNTIL (VARIANCE(S1) LT 10000) || (CC GT 3000)
PRINT,VARIANCE(S1),CC
S1=CLEAN+S1

WINDOW,0,Xsize=283,Ysize=283
TVSCL,S1
ZOOM,FACT=2,XSIZE=566,YSIZE=566


END
